<?php
/*******************************
 * 依据传入的视频地址获取视频缩略图 视频名称
 * 传入SWF地址支持 优酷，土豆网，Ku6，56，激动网的视频
 * 传入HTML地址支持 优酷，土豆网，Ku6，激动网，56的视频
 * 使用方法
 * $video = new UCLibVideoInfoAcquisition($videourl);
 * $videoInfo = $video->VideGet();
 * @string $videourl: 视频的URL地址
 * 返回信息
 * array('flashvar'=>视频编码,'title'=>视频标题,'imageurl'=>视频图片地址,'swfurl'=>视频地址);
 * 
*********************************/
class UCLibVideoInfoAcquisition{
  var $link,$host,$videInfo;
  var $linkarray = array('youku.com','56.com','ku6.com','tudou.com','joy.cn');
  
  function __construct($link) {
    if(empty($link)){
      return false;
    }
    $link = trim($link);
    $turl = parse_url($link);
    $host = $turl['host'];
    $h = explode('.', $host);
    if(false === strpos($host, '.') || preg_match("/^(\d+\.){3}(\d+)$/", $host)){
      $root = $host;
     }elseif(preg_match("/(?:com|tel|mobi|net|org|asia|me|tv|biz|cc|name|info)(?:\.\w{2})$/", $host, $match)){
      array_pop($h);
      array_pop($h);
      $root = array_pop($h) . '.' . $match[0];
     }elseif(preg_match("/(?:\w{2,4})$/", $host, $match)){
      array_pop($h);
      $root = array_pop($h) . '.' . $match[0];
     }
     if(!in_array($root,$this->linkarray)){
      return false;
    }
    $this->host = $root;
    $this->link = $link;
  }
  
  //判断传入的数据是HTML还是SWF
  function VideGet(){
    $str = pathinfo($this->link);
    $str = explode("?",$str['extension']);
    $str = $str[0];
    if($str == 'swf' || ($str != 'htm' && $str != 'html' && $str != 'do')){
      $this->SwfToHtml();
    }
    $info = $this->getVideoInfo();
    return $info;
  }
  
  //依据SWF的地址获取HTML地址信息
  function SwfToHtml(){
    if('youku.com' == $this->host) {
      $pieces = explode("/", $this->link);
      $this->link = 'http://v.youku.com/v_show/id_'.$pieces[5].'.html';
    }elseif('ku6.com' == $this->host){
      $pieces = explode("/", $this->link);
      $this->link = 'http://v.ku6.com/show/'.$pieces[4].'.html';
    }elseif('tudou.com' == $this->host){
      $pieces = explode("/", $this->link);
      if($pieces[3] == 'l'){
      	$Location = $this->curlLocation($this->link);
       	preg_match("/lid=(\d{1,20})/i",  $Location, $url);
      	$this->link = 'http://www.tudou.com/playlist/playindex.do?lid='.$url[1];
      }else{
      	if(strlen($pieces[4]) < 8){
      		$flashvar = $pieces[5];
      	}else{
      		$flashvar = $pieces[4];
      	}
      	$this->link = 'http://www.tudou.com/programs/view/'.$flashvar.'/';
      }
    }elseif('56.com' == $this->host){
      preg_match_all("/\/([\w\-]+)\.swf/", $this->link, $matches);
      $pieces = explode("_", $matches[1][0]);
      $this->link = 'http://www.56.com/u85/v_'.$pieces[1].'.html';
     }elseif('joy.cn' == $this->host){
      preg_match_all("/\/([\w\-]+)\.swf/", $this->link, $matches);
      $pieces = explode("_", $matches[1][0]);
      if($pieces[0] == '51com'){
        $this->link = 'http://news.joy.cn/video/'.$pieces[1].'.htm';
      }elseif($pieces[0] == '51comvodsp'){
        $this->link = 'http://news.joy.cn/video/'.$pieces[4].'.htm';
      }else{
        $this->link = 'http://news.joy.cn/video/'.$pieces[0].'.htm';
      }
    }
  }
  
  //依据HTML地址获取视频信息
  function getVideoInfo(){
    $return = array();
    if('youku.com' == $this->host) {
      //分析视频网址，获取视频编码号
      preg_match_all("/id\_(\w+)[\=|.html]/", $this->link, $matches);
      if(!empty($matches[1][0])) {
        $return['flashvar'] = $matches[1][0];
      }
      //获取视频页面内容，存与$text中
      $text= @file_get_contents($this->link);
	  if(!$text){
		 $return['flashvar'] = '';
		 return $return;
	  }
      $text = str_replace("\n", "", $text);
      //获取视频标题
      preg_match("/<title>(.*?) - (.*)<\/title>/i",  $text, $title);
      //获取优酷网上某一视频对应的视频截图，经分析，视频的截图的图片地址在该视频页面html代码里以<li class="download"></li>标记里的最后一个http://vimg....例如http://vimg20.yoqoo.com/0100641F4649B9D27344B00131FBB6AFDF5175-7D35-930B-E43C-99C59F918E00
      preg_match_all("/<li class=\"download\">(.*?)<\/li>/i",$text,$match2);
      $pieces = explode("|", $match2[1][0]);
      //preg_match("/http:\/\/vimg(.*)\|\"\>/",$match2[1][0],$imageurl);
      //if (!empty($imageurl[1])) {
        //$return['imageurl'] = 'http://vimg'.$imageurl[1];
      //}
      if(!empty($pieces[8])){
        $return['imageurl'] = $pieces[8];
      }
      if (!empty($title)) {
        $return['title'] = $title[1];
      }
      $return['swfurl'] = 'http://player.youku.com/player.php/sid/'.$return['flashvar'].'/v.swf';
    } elseif('ku6.com' == $this->host) {
      // http://v.ku6.com/show/bjbJKPEex097wVtC.html
      // http://v.ku6.com/special/index_3628020.html
      //对于酷6网，末尾以index_开头的地址需要另外分析其视频编码
      $text= @file_get_contents($this->link);
	  if(!$text){
		 $return['flashvar'] = '';
		 return $return;
	  }
      $text = str_replace("\n", "", $text);
      preg_match_all("/\/([\w\-]+)\.html/", $this->link, $matches);
      if(1 > preg_match("/\/index_([\w\-]+)\.html/", $this->link) && !empty($matches[1][0])) {
        $return['flashvar'] = $matches[1][0];
      }else{
        preg_match_all("/refer\/(.*)\/v.swf/",$text,$videourl);
        $return['flashvar'] = $videourl[1][0];
      }
      preg_match("/<title>(.*?) - (.*)<\/title>/i",  $text, $title);
      //经分析，酷六的视频截图地址在视频页面的<span class="s_pic“></span>标签之间
      preg_match_all("/<span class=\"s_pic\">(.*?)<\/span>/",$text,$imageurl);
      if (!empty($imageurl[1][0])) {
        $return['imageurl'] = $imageurl[1][0];
      }
      if (!empty($title)) {
        $return['title'] = $title[1];
      }
      $return['swfurl'] = 'http://player.ku6.com/refer/'.$return['flashvar'].'/v.swf';
    }elseif ('tudou.com' == $this->host){
      //分析视频网址，获取视频编码号
      preg_match_all("/view\/([\w\-]+)\//", $this->link, $matches);
      if(!empty($matches[1][0])) {
        $return['flashvar'] = $matches[1][0];
      }
      //获取视频页面内容，存与$text中
	  $text= @file_get_contents($this->link);
	  if(!$text){
		 $return['flashvar'] = '';
		 return $return;
	  }
      $text = str_replace("\n", "", $text);
      //方法二
      if(empty($return['flashvar'])){
      	preg_match("/lid_code = \'(.*?)\'/i",$text,$flashvar);
      	$return['flashvar'] = $flashvar[1];
      }    	      	
      preg_match("/<title>(.*?)_(.*)<\/title>/i",  $text, $title);
      if (!empty($title)) {
        $return['title'] = $title[1];
      }
      preg_match_all("/<span class=\"s_pic\">(.*?)<\/span>/i",$text,$imageurl);
      if (!empty($imageurl[1][0])) {
        $return['imageurl'] = $imageurl[1][0];
      }
      if (!empty($title)) {
        $return['title'] = $title[1];
      }
      //preg_match("/<param name=\"movie\" value=\"(.*?)\">/i",$text,$swfurl);
      preg_match("/<input type=\"hidden\" value='<embed src=\"(.*?)\" type=/i",$text,$swfurl);
      if(count($swfurl) == 0){
      	$return['swfurl'] = "http://www.tudou.com/v/".$return['flashvar'];
      }else{
      	$return['swfurl'] = $swfurl[1];
      }
    }elseif('56.com' == $this->host){
      //分析视频网址，获取视频编码号
      preg_match_all("/v\_(\w+)[\=|.html]/", $this->link, $matches);
      if(!empty($matches[1][0])) {
        $return['flashvar'] = $matches[1][0];
      }
      //获取视频页面内容，存与$text中
      $text= @file_get_contents($this->link);
	  if(!$text){
		 $return['flashvar'] = '';
		 return $return;
	  }
      $text = str_replace("\n", "", $text);
      //获取视频标题
      preg_match("/<title>(.*?) - (.*)<\/title>/i",  $text, $title);
      preg_match_all("/\"img\":\"(.*?)\"\};/i",$text,$match2); //提取
      if(!empty($match2[1])){
        $return['imageurl'] = stripslashes($match2[1][0]);
      }
      if (!empty($title)) {
        $return['title'] = $title[1];
      }
      $return['swfurl'] = 'http://player.56.com/v_'.$return['flashvar'].'.swf';
    }elseif('joy.cn' == $this->host){
      //分析视频网址，获取视频编码号
      preg_match_all("/\/([\w\-]+)\.htm/", $this->link, $matches);
      if(!empty($matches[1][0])) {
        $return['flashvar'] = $matches[1][0];
      }
      //获取视频页面内容，存与$text中
	  $text= @file_get_contents($this->link);
	  if(!$text){
		 $return['flashvar'] = '';
		 return $return;
	  }
      $text = str_replace("\n", "", $text);
      //获取视频标题
      preg_match("/<title>(.*?)-(.*)<\/title>/i",  $text, $title);
      preg_match_all("/Cover:\"(.*?)\",/i",$text,$match2); //提取
      if(!empty($match2[1])){
        $return['imageurl'] = stripslashes($match2[1][0]);
      }
      if (!empty($title)) {
        $return['title'] = $title[1];
      }
      $return['swfurl'] = 'http://client.joy.cn/flvplayer/'.$return['flashvar'].'_1_0_1.swf';
    }
    //if(!$this->is_gb2312($return['title'])){
      //$return['title'] = iconv("UTF-8","GBK",$return['title']);
    //}
    $coding = $this->is_charset($text);
    if($coding != "UTF-8" || $coding != "utf-8"){
      $return['title'] = iconv($coding,"UTF-8",$return['title']);
    }
    $return['title'] = trim(preg_replace('/在线观看/','',$return['title']));
    return $return;
  }
  
  //判断字符串编码
  function is_gb2312($str){
    for($i=0; $i<strlen($str); $i++) {
      $v = ord( $str[$i] );
      if( $v > 127) {
        if(($v >= 228) && ($v <= 233)){
          if( ($i+2) >= (strlen($str) - 1)) return true;  // not enough characters
          $v1 = ord( $str[$i+1] );
          $v2 = ord( $str[$i+2] );
          if( ($v1 >= 128) && ($v1 <=191) && ($v2 >=128) && ($v2 <= 191) ) // utf编码
          return false;
          else
          return true;
        }
      }
    }
    return true;
  }
  //获取网页charset
  function is_charset($html){
    preg_match("/<meta.+?charset=([-\w]+)/i",$html,$charset);
    if(empty($charset)){
    	preg_match("/<meta.+?charset=\"([-\w]+)\"/i",$html,$charset);
    }
    if(empty($charset)){
    	preg_match("/<meta.+?charset=\'([-\w]+)\'/i",$html,$charset);
    }
    return $charset[1];    
  }
  
  //获取网页的header信息，并返回Location的参数
  function curlLocation($url){
  	$ch = curl_init();
  	curl_setopt($ch, CURLOPT_URL, $url);
  	curl_setopt($ch, CURLOPT_HEADER, 1);
  	curl_setopt($ch, CURLOPT_NOBODY, 1);
  	curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
  	$get_contents = curl_exec($ch);
  	if ($error = curl_error($ch) ) {
  		//出错处理
  		return false;
  	}
  	curl_close($ch);
  	preg_match("/Location:(.*)/i",  trim($get_contents), $charset);
  	return $charset[1];
  }
  
}
?>